/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_components/zhiqim_manager.htm
 *
 * Zhiqim Manager is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.manager.presenter;

import java.util.List;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.annotation.AnIntercept;
import org.zhiqim.httpd.validate.ones.IsNotEmpty;
import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.json.Jsons;
import org.zhiqim.kernel.util.Arrays;
import org.zhiqim.kernel.util.Lists;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.manager.ZmrConstants;
import org.zhiqim.manager.dao.ZmrOperatorDao;
import org.zhiqim.manager.dbo.ZmrOperator;
import org.zhiqim.manager.dbo.ZmrRole;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Updater;

/**
 * 角色控制器
 *
 * @version v1.0.0 @author zouzhigang 2017-6-6 新建与整理
 */
@AnAlias("ZmrRolePresenter")
@AnIntercept("chkZmrLogin")
public class ZmrRolePresenter implements ZmrConstants
{
    /**
     * 增加操作员多个角色，参数统一在request中
     * 
     * @param request       请求
     * @throws Exception    异常
     */
    public static void doAddOperatorRoles(HttpRequest request) throws Exception
    {
        request.addValidate(new IsNotEmpty("operatorCode", "请选择操作员"));
        request.addValidate(new IsNotEmpty("roleIds", "请选择一个角色"));
        if (!request.chkValidate())
        {
            request.setResponseError(request.getAlertMsg());
            return;
        }
        
        //判断操作员是否存在
        String operatorCode = request.getParameter("operatorCode");
        ZmrOperator operator = ORM.table().item(ZmrOperator.class, operatorCode);
        if (operator == null)
        {
            request.returnHistory("您选择的操作员不存在，请选择一个有效的操作员");
            return;
        }
        
        //判断角色是否存在
        String roleIds = request.getParameter("roleIds");
        String[] roleIdArr = Arrays.toStringArray(roleIds);
        for (String roleId : roleIdArr)
        {
            if (Validates.isEmpty(operator.getOperatorRole()))
                operator.setOperatorRole(roleId);
            else if (!operator.getOperatorRole().contains(roleId))
                operator.setOperatorRole(operator.getOperatorRole() + "," + roleId);
        }
        
        Updater updater = new Updater();
        updater.addField("operatorRole", operator.getOperatorRole());
        updater.addMust("operatorCode", operatorCode);
        
        ORM.table().update(ZmrOperator.class, updater);
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "增加操作员角色关联", Jsons.toString("operatorCode", operatorCode, "roleIds", roleIds));
    }
    
    /**
     * 删除操作员角色，参数统一在request中
     * 
     * @param request       请求
     * @throws Exception    异常
     */
    public static void doDeleteOperatorRole(HttpRequest request) throws Exception
    {
        request.addValidate(new IsNotEmpty("operatorCode", "请选择一个操作员"));
        request.addValidate(new IsNotEmpty("roleId", "请选择一个角色"));
        if (!request.chkValidate())
        {
            request.setResponseError(request.getAlertMsg());
            return;
        }
        
        String operatorCode = request.getParameter("operatorCode");
        ZmrOperator operator = ORM.table().item(ZmrOperator.class, operatorCode);
        if (operator == null)
        {
            request.setResponseError("请选择一个有效的操作员");
            return;
        }
        
        //删除对应的角色
        long roleId = request.getParameterLong("roleId");
        String operatorRole = operator.getOperatorRole();
        List<Long> operatorRoleList = Lists.toLongList(operatorRole);
        operatorRoleList.remove(roleId);
        
        operatorRole = Lists.toString(operatorRoleList);
        
        Updater updater = new Updater();
        updater.addField("operatorRole", operatorRole);
        updater.addMust("operatorCode", operatorCode);
        
        ORM.table().update(ZmrOperator.class, updater);
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "取消操作员角色关联", Jsons.toString("operatorCode", operatorCode, "roleId", roleId));
    }
    
    /**
     * 在角色中增加多个操作员，参数统一在request中
     * 
     * @param request       请求
     * @throws Exception    异常
     */
    public static void doAddRoleOperator(HttpRequest request) throws Exception
    {
        request.addValidate(new IsNotEmpty("roleId", "请选择一个角色"));
        request.addValidate(new IsNotEmpty("operatorCodes", "请选择操作员"));
        if (!request.chkValidate())
        {
            request.setResponseError(request.getAlertMsg());
            return;
        }
        
        //判断角色是否存在
        long roleId = request.getParameterLong("roleId");
        ZmrRole role = ORM.table().item(ZmrRole.class, roleId);
        if (role == null)
        {
            request.returnHistory("您选择的角色不存在，请选择一个有效的角色");
            return;
        }
        
        String operatorCodes = request.getParameter("operatorCodes");
        String[] operatorCodeArr = Arrays.toStringArray(operatorCodes);
        for (String operatorCode : operatorCodeArr)
        {
            ZmrOperator operator = ORM.table().item(ZmrOperator.class, operatorCode);
            if (operator == null)
                continue;
        
            //操作员角色
            String operatorRole = operator.getOperatorRole();
            if (Validates.isEmpty(operatorRole))
                operatorRole = "" + roleId;
            else
                operatorRole += "," + roleId;
            
            Updater updater = new Updater();
            updater.addField("operatorRole", operatorRole);
            updater.addMust("operatorCode", operatorCode);
            
            ORM.table().update(ZmrOperator.class, updater);
        }
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "增加操作员角色关联", Jsons.toString("operatorCodes", operatorCodes, "roleId", roleId));
    }
}
